package org.example.leetcode;

import java.util.Stack;

/**
 * @version 1.0.0
 * @author: lynn
 * @description: 由leetcode54 变种而来，加了stack进行赋值而已
 * @date: 2021/8/10 12:29
 */
public class LC59 {
    public static void main(String[] args) {

    }

    public int[][] generateMatrix(int n) {
        int[][] res=new int[n][n];
        Stack<Integer> stack=new Stack<>();
        for (int i = n*n; i >=1 ; i--) {
            stack.push(i);
        }
        int row=n;
        int col=n;
        int left=0;
        int right=col-1;
        int up=0;
        int down=row-1;
        while (stack.size()>0){
            //从左到右
            for (int i = left; i <=right ; i++) {
                if (stack.size()==0) break;
                res[up][i]=stack.pop();
            }
            up++;
            //从上到下
            for (int i = up; i <=down ; i++) {
                if (stack.size()==0) break;
                res[i][right]=stack.pop();
            }
            right--;
            //从右到左
            for (int i = right; i >=left ; i--) {
                if (stack.size()==0) break;
                res[down][i]=stack.pop();
            }
            down--;
            //从下到上
            for (int i = down; i >=up; i--) {
                if (stack.size()==0) break;
                res[i][left]=stack.pop();
            }
            left ++;
        }
        return res;
    }
}
